using System;
using System.Collections.Generic;
using System.Text;
using IPSim.Core;
using IPSim.Data;
using IPSim.Util;

namespace IPSim.Devices
{
    class Hub: IConnectable
    {

        private TransportEthernetFrame m_frameReceiver;
        private TransportEthernetFrame m_frameSender;

        public Hub()
        {
            m_frameReceiver = new TransportEthernetFrame(receiveFrame);
            m_frameSender = null;
            Logger.log("Hub created!");
        }

        public void receiveFrame(TransportEthernetFrame receiver, EthernetFrame frame)
        {
            //m_frameSender(m_frameReceiver, frame);
            Delegate[] invocationList = m_frameSender.GetInvocationList();
            for (int i = 0; i < invocationList.Length; ++i)
            {
                // Ovo (TransportEthernetFrame) je dodato zbogwarninga, moguci izvor problema :)
                if (receiver != (TransportEthernetFrame) invocationList[i]) ((TransportEthernetFrame)invocationList[i])(m_frameReceiver, frame);                                
            }
            Logger.log("[Hub] Ethernet frame forwarded!");
        }

        #region IConnectable Members

        public void Connect(IConnectable device, byte num)
        {
            m_frameSender += device.FrameReceiver1;
            Logger.log("Connected to Hub:" + device.GetType().Name);
        }

        public void Disconnect(IConnectable device, byte num)
        {
            m_frameSender -= device.FrameReceiver1;
        }

        public TransportEthernetFrame FrameReceiver1
        {
            get { return m_frameReceiver; }
        }
        public TransportEthernetFrame FrameReceiver2
        {
            get { return null; }
        }
        public TransportEthernetFrame FrameReceiver3
        {
            get { return null; }
        }
        public TransportEthernetFrame FrameReceiver4
        {
            get { return null; }
        }

        #endregion
    }
}
